package cn.three.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;

import cn.three.biz.ShopdaoBiz;
import cn.three.eneity.Admin;
import cn.three.eneity.Classify;
import cn.three.eneity.Shop;

import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;






public class AddShopServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	 request.setCharacterEncoding("UTF-8");
	 response.setContentType("text/html; charset=UTF-8");
	PageContext pagecontext= JspFactory.getDefaultFactory().getPageContext(this, request, response,
			null, true, 8192, true);

	SmartUpload su=new SmartUpload();
	su.initialize(pagecontext);
	su.setCharset("UTF-8");
	try {
		su.save("upfiles");
		su.upload();
	} catch (Exception e) {
		e.printStackTrace();
	}
	
	
	Request req=su.getRequest();
	
			Shop shop=new Shop();
			
			String shopname=req.getParameter("shopname");
	        shop.setShopName(shopname);
			
			String shoppr= req.getParameter("shopprice");
	        int shopprice = Integer.parseInt(shoppr);
	        shop.setShopPrice(shopprice);
	        
	        String shopdetails= req.getParameter("shopdetalis");
	        shop.setShopDetails(shopdetails);
	        
	        String shopcou= req.getParameter("shopcount");
	        int shopcount = Integer.parseInt(shopcou);
	        shop.setShopCount(shopcount);
	        
	        String pic =su.getFiles().getFile(0).getFileName();
	        
	        shop.setShopPic(pic);
	        HttpSession session=request.getSession();
	        Admin admin = (Admin) session.getAttribute("admin");
	        int adminid=admin.getAdminId();
	        admin.setAdminId(adminid);
	        shop.setAdmin(admin);
	        
	        shop.setAdmin(admin);
	        String classifyname =req.getParameter("classifyname");
	        int classifyid = Integer.parseInt(classifyname);
	        Classify  classify=new Classify();
		    classify.setClassifyId(classifyid);
		    shop.setClassify(classify);  
		    
			       ShopdaoBiz shopBiz = new ShopdaoBiz();
			   int s =shopBiz.addShop(shop);
			          if (s>0){
			         response.sendRedirect("shop.jsp");
			          }else{
			          	response.sendRedirect("shop.jsp");
			          	}
		        
		}
		
	

		
		
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
	     



